#!/bin/bash

# Defining variables
UA_PORT="8568"
INSTALL_OPTION="-D"
SWAN_INSTALL_FILE="swan-install.tar"

# Check if java is installed on this machine.
function check_java() 
{
	if [ -z "$(which java)" ]; then
		echo
		echo "javaVM is not in your path. Please type your PATH to java now: [/usr/local/jdk/bin]"
		read java
		if [ -z $java ]; then
		   java="/usr/local/jdk/bin"
		fi
		export PATH="$java":"$PATH"
	fi
	
	if [ -z "$(which java)" ]; then
		echo
		echo "You do not have the Java Virtual Machine (JVM) installed in your computer"  
		echo "or you specified a wrong path for JVM."
		echo "You must install the JVM to be able to install SWAN."
	    echo "Please install it or contact the system administrator."
	    exit 1
	fi
}

# Check if vserver is installed on this machine.
function check_verser()
{
	if [ -z "$(which vserver)" ]; then
		echo
		echo "vserver is not in your path. Please type your PATH to vserver now: [/usr/sbin]"
		read vserver
		if [ -z $vserver ]; then
		   vserver="/usr/sbin"
		fi
		export PATH="$vserver":"$PATH"
	fi
	
	if [ -z "$(which vserver)" ]; then
		echo
		echo "You do not have the Vserver installed in your computer"
		echo "or you  specified a wrong path for vserver."
		echo "You have to install the Vserver to be able to install OurGrid-VServer."
        echo "Please install it or contact the system administrator."
        exit 1
	fi
}

# Check if sudo is installed on this machine.
function check_sudo()
{
	if [ -z "$(which sudo)" ]; then
		echo 
		echo "You have to install the sudo package to be able to install OurGrid-VServer."
		echo "Please install it or contact the system administrator."
		exit 1
	fi

}

# main() - Checking for dependencies.
echo
echo "================================="
echo "   Ready to begin installation   "
echo "================================="
echo
echo "==========================="
echo "   Checking dependencies   "
echo "==========================="
echo

echo -n "Checking for Java... "
check_java
echo "OK"
echo

echo -n "Checking for Sudo... "
check_sudo
echo "OK"
echo

echo -n "Checking for vserver... "
check_verser
echo "OK"
echo

#

function check_user()
{
	su $swanname -c true &> /dev/null
	if [ $? -ne 0 ]; then
		echo
		echo "User $swanname does not exist, you have to create it before installation or select another user." 
		exit 1
	fi
}

function check_kernel()
{
	if [ ! "$(find /boot | grep "$vserver")" ]; then
		echo 
		echo "We could not find your vserver kernel patch."
		echo "Please find out the name of your vserver kernel to continue the installation."
		exit 1
	fi
}

# main() - Configuring Swan.
echo
echo "======================"
echo "   Configuring Swan   "
echo "======================"
echo

# Configuring Swan user.
echo
echo "Please enter the username for running Swan: [swan]"
read swanname
if [ -z $swanname ]; then
	swanname="swan"
fi

check_user

# Configuring Xen kernel.
echo
echo "The XEN Virtual Machine has two kernels: one for the domain 0, one for the domain U."
echo "Please verify the name of your kernel for the domain U in /boot directory."
echo
echo "Please enter the U kernel name or we cannot continue the installation: [vmlinuz-2.6-xenU]"
read kernel
if [ -z $kernel ]; then
	kernel="vmlinuz-2.6-xenU"
fi

check_kernel

# Configuring storage and playpen size.
echo
echo "OurGrid will need disk space to store data and tasks results. Due to data intensive applications, we recommend the default size (512 MB) to be used."
echo "If you want to chance the disk space in future you could see how to do it in OurGrid Online Manual at:"
echo "http://ourgrid.org " 
echo
echo "Please enter the amount of size that OurGrid could use, the value will be specified in megabytes: [512]"

read size
if [ -z $size ]; then
	size="512"
fi

# Selecting installation place
echo
echo "Select the directory you want to install SWAN. [$(pwd)] "

read pwdl
if [ -z $pwdl ]; then
	pwdl=`pwd`
fi

# Verifying if install directory exists
echo
echo "Verifying if $pwdl exists..."
ls $pwdl > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
	echo
	echo "Directory $pwdl not exists..."
	echo "Creating directory $pwdl..."
	mkdir $pwdl
fi
echo "OK"
echo

# Installing Swan default files.
tar xvf $SWAN_INSTALL_FILE -C $pwdl
if [ $? -ne 0 ]; then
	echo
	echo "You do not have permission to install SWAN at $pwdl."
	exit 1
fi

# Creating log and mnt directories
cd $pwdl/swan

mkdir log 2> /dev/null
mkdir mnt 2> /dev/null

# Configuring Xen config
sed -e "s/%KERNEL%/$kernel/g" config > config.$$
cp config.$$ config
sed -e "s|%DIR%|$pwdl/swan|g" config > config.$$
cp config.$$ config
rm config.$$

# Configuring Swan functions
sed -e "s/%SIZE%/$size/g" bin/swan_functions > bin/swan_functions.$$
cp bin/swan_functions.$$ bin/swan_functions
sed -e "s|%DIR%|$pwdl/swan|g" bin/swan_functions > bin/swan_functions.$$
cp bin/swan_functions.$$ bin/swan_functions
rm bin/swan_functions.$$

# main() - Installing UserAgent
echo
echo "=========================="
echo "   Installing UserAgent   "
echo "=========================="
echo
echo -n "Do you have an OurGrid UserAgent installed on your machine? (y/[n])"

read answer
if [ -z $answer ]; then
	answer="n"
fi

if [ "$answer" = "n" ]; then
    echo
    echo "Installing the OurGrid UA"

	tar zxvf src/useragent-*
	HOSTNAME=`hostname -f`
	sed -e "s/localhost/$HOSTNAME/g" useragent/ua.properties -e "s/1099 /$UA_PORT/g" > useragent/ua.properties.$$ -e "s/#ua.name/ua.name/g" > useragent/ua.properties.$$ -e "s/#ua.port/ua.port/g" > useragent/ua.properties.$$
	mv useragent/ua.properties.$$ useragent/ua.properties

	# Changing the owner of the user agent directories
	chown -R $swanname $pwdl/swan/useragent
	su -c "export PATH=$java:$PATH;useragent/useragent start" $swanname
	echo
else
	echo "Please be sure that your OurGrid UserAgent is correctly installed in XEN environment."
	echo
fi

# main() - Installing Swan
echo "====================="
echo "   Installing SWAN   "
echo "====================="

install $INSTALL_OPTION -o root -g root -m 755 bin/swan_initvm /usr/bin/swan_initvm
install $INSTALL_OPTION -o root -g root -m 755 bin/swan_stagein /usr/bin/swan_stagein
install $INSTALL_OPTION -o root -g root -m 755 bin/swan_stageout /usr/bin/swan_stageout
install $INSTALL_OPTION -o root -g root -m 755 bin/swan_killvm /usr/bin/swan_killvm
install $INSTALL_OPTION -o root -g root -m 755 bin/swan_functions /usr/bin/swan_functions

# Adding permissions to sudoers file

if [ -z "$(grep "$swanname ALL=NOPASSWD:/bin/mount *$pwdl/swan/src/vmdom1 $pwdl/swan/mnt" /etc/sudoers)" ]; then
	echo "$swanname ALL=NOPASSWD:/bin/mount *$pwdl/swan/src/vmdom1 $pwdl/swan/mnt" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/bin/mount *$pwdl/swan/src/vmdom2 $pwdl/swan/mnt" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/bin/mount *$pwdl/swan/src/vmdom2 $pwdl/swan/mnt" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/bin/umount *$pwdl/swan/mnt" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/bin/umount *$pwdl/swan/mnt" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/usr/sbin/xm create *" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/usr/sbin/xm create *" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/usr/sbin/xm list" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/usr/sbin/xm list" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/usr/sbin/xm destroy *" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/usr/sbin/xm destroy *" >> /etc/sudoers
fi

if [ -z "$(grep "$swanname ALL=NOPASSWD:/sbin/e2fsck *" /etc/sudoers)" ]; then
        echo "$swanname ALL=NOPASSWD:/sbin/e2fsck *" >> /etc/sudoers
fi


# Changing the owner of the swan scripts.
chown $swanname /usr/bin/swan_stagein
chown $swanname /usr/bin/swan_stageout
chown $swanname /usr/bin/swan_killvm
chown $swanname /usr/bin/swan_initvm
chown $swanname /usr/bin/swan_functions

# Changing the owner of the swan directories
chown -R $swanname $pwdl/swan/

echo
echo "====================================="
echo "   You're done. SWAN is installed."
echo "====================================="
echo 
